import pygame
import numpy as np
import yaml
from collections import namedtuple
import os

# global variable
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 600
MYSPEED = 5
INIT_X, INIT_Y = 420, 360
BG_COLOR = pygame.Color(0, 0, 0)
RECOVERBUFFER = 20
GAMESETTINGDIR = "map"

######################
Setting = namedtuple("setting", ['enemyTankCount',
                                 'recoverTimes',
                                 'bg',
                                 'map',
                                 'diy',
                                 'target'])


def parser(gameIndex):
    f = open(os.path.join(GAMESETTINGDIR, f"level_{gameIndex}.yaml"), 'r')  # 0 is diy
    args = yaml.load(f, yaml.SafeLoader)
    setting = Setting(**args)
    f.close()
    Map.enemyTankCount = setting.enemyTankCount
    Map.background = setting.bg
    try:
        Map.diyMap = np.load(setting.map).T
    except:
        pass
    Map.recoverTimes = setting.recoverTimes
    Map.diy = setting.diy
    Map.target = setting.target


######################

class Map:
    window = None
    myTank = None
    myBox = None
    # 音量
    volume = 50  # volume default
    # 存储敌方坦克列表
    enemyTankList = []
    # 定义敌方坦克数量
    enemyTankCount = 5
    # 存储我方子弹的列表
    myBulletList = []
    # 存储敌方子弹
    enemyBulletList = []
    # 存储爆炸对象
    explodeList = []
    # 存储墙壁的列表
    wallList = []
    # 储存道具列表
    itemList = []
    # 音乐
    bgm = None
    # 地图矩阵
    diyMap = np.load("./map/level_0.npy")
    diyMap = diyMap.T

    recoverTimes = 5

    Time = 0

    diy = False

    recoverBuffer = RECOVERBUFFER

    enemy_attributes = [3, 6, 5]

    player_attributes = [4, 6, 3]

    target = 0

    server = True

    background = './img/bg.jpg'

    uid = None

    def __init__(self):
        pass
